//
//  ZPMineClassChangeRecordVC.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/1/13.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPMineClassChangeRecordVC.h"
#import "ZPEmptyGradeView.h"


#import "ZPAllClassViewController.h"
#import "ZPTiaoClassViewController.h"
#import "ZPZhuanClassViewController.h"
#import "ZPTuiClassViewController.h"



#define classSateScrollV_H 44.

@interface ZPMineClassChangeRecordVC ()<UIScrollViewDelegate>
{
    MBProgressHUD *hud;
    ZPEmptyGradeView *emptyFooterView;
    
}
// 课程状态滚动图
@property (strong, nonatomic) UIScrollView       *classSateScrollV;

// 标题下划线
@property (nonatomic,  weak)  UIView             *lineView;

// 主题内容滚动图
@property (strong, nonatomic) UIScrollView       *classContensScrollV;

// 选中的按钮
@property (nonatomic, weak) UIButton             *selectButton;

// 装载按钮的数组
@property (nonatomic, strong) NSMutableArray     *titleButtons;

@end

@implementation ZPMineClassChangeRecordVC


- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil)
    {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}



- (UIScrollView *)classSateScrollV
{
    
    if (_classSateScrollV == nil)
    {
        _classSateScrollV = [[UIScrollView alloc] init];
        _classSateScrollV.backgroundColor = UIColorFromRGB(0xF9F9F9);
        _classSateScrollV.frame = CGRectMake(0, 0, Main_Screen_Width, classSateScrollV_H);
        _classSateScrollV.scrollsToTop = NO;
        NSInteger VCcount = self.childViewControllers.count;
        CGFloat btnW =  (Main_Screen_Width - 3) / 4;
        CGFloat btnH =  _classSateScrollV.xj_height;
        for (int i = 0; i < VCcount; i++)
        {
            UIButton *classStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            classStateBtn.tag = i;
            // 按钮的标题是自控制器的标题
            UIViewController * VC = self.childViewControllers[i];
            [[ZPShareView sharedSingleView] setupButton:classStateBtn backgroundColor:clear_color image:nil title:VC.title titltColor:UIColorFromRGB(0X999999) titleFont:14.];
            // 设置按钮的位置
            classStateBtn.frame = CGRectMake(i * (btnW + 1), 0, btnW, btnH);
            // 点击
            [classStateBtn addTarget:self action:@selector(clickTitleBotton:) forControlEvents:UIControlEventTouchUpInside];
            // 加载状态按钮
            [_classSateScrollV addSubview:classStateBtn];
            
            // 添加状态按钮之间的竖线
            if (i > 0 ) {
                UIView  *SeparateView = [[UIView alloc] init];
                SeparateView.backgroundColor = UIColorFromRGB(0xEEEEEE);
                SeparateView.frame = CGRectMake(i * (btnW + 1), (btnH-14)/2, 1, 14);
                [_classSateScrollV addSubview:SeparateView];
            }
            
            // 添加下划线
            // 下划线的宽度    = 标题文字的宽度
            // 下划线的中心点x = 标题按钮中心点x
            if (i == 0)
            {
                
                CGFloat h = 1.5;
                CGFloat y = 40.0;
                UIView * lineView = [[UIView alloc] init];
                // 位置尺寸
                lineView.xj_height = h;
                // 先计算标题按钮的尺寸,再给下滑线标签赋值
                [classStateBtn.titleLabel sizeToFit];
                // 宽
                lineView.xj_width = classStateBtn.xj_width / 2;
                // 中心点x
                lineView.xj_centerX = classStateBtn.xj_centerX;
                // y点坐标
                lineView.xj_y = y;
                // 下划线背景色
                lineView.backgroundColor = zMainColor;
                // 引用
                _lineView = lineView;
                // 循环加载下划线
                [_classSateScrollV addSubview:lineView];
                // 点击标题按钮方法
                [self clickTitleBotton:classStateBtn];
            }
            // 标题按钮循环添加到数组中
            [self.titleButtons addObject:classStateBtn];
        }
    }
    return _classSateScrollV;
}




- (UIScrollView *)classContensScrollV
{
    if (_classContensScrollV == nil) {
        _classContensScrollV = [[UIScrollView alloc] init];
        _classContensScrollV.frame = CGRectMake(0,classSateScrollV_H, Main_Screen_Width, Main_Screen_Height - classSateScrollV_H - 64);
        _classContensScrollV.contentSize = CGSizeMake(self.childViewControllers.count * Main_Screen_Width, 0);
        _classContensScrollV.bounces = YES;
        _classContensScrollV.pagingEnabled = YES;
        _classContensScrollV.showsHorizontalScrollIndicator = NO;
        _classContensScrollV.delegate = self;
        for (int i = 0; i < self.childViewControllers.count; i++)
        {
            UIView * childView = self.childViewControllers[i].view;
            childView.tag = i;
            childView.frame = CGRectMake(Main_Screen_Width * i, 0, Main_Screen_Width, _classContensScrollV.xj_height);
            //            childView.backgroundColor = zRandomColor;
            [_classContensScrollV addSubview:childView];
        }
    }
    return _classContensScrollV;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    
    [self setupAllChildViewController];
    [self setupClassSateScrollV];
    [self setupClassContensScrollV];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"变更记录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    
}

- (void)backButtonClick
{
    if (_needPopToRoot) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}



- (void)setupClassSateScrollV
{
    [self.view addSubview:self.classSateScrollV];
}
- (void)setupClassContensScrollV
{
    [self.view addSubview:self.classContensScrollV];
}



#pragma mark - <点击标题按钮>
- (void)clickTitleBotton:(UIButton *)sender {
    
    NSInteger i = sender.tag;
    // 重复点击按钮的时候刷新当前界面
    if (sender == _selectButton) {
        
        ZPLog(@"刷新界面");
    }
    [self selectedClassStateBtnClick:sender];
    
    // collectionView修改偏移量
    [self.classContensScrollV setContentOffset:CGPointMake(Main_Screen_Width * i, 0) animated:YES];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 获取角标
    NSInteger i = scrollView.contentOffset.x / Main_Screen_Width;
    
    // 获取选中按钮
    UIButton *selectButton = self.titleButtons[i];
    
    // 调用选中标题方法,实现下划线跟随crollView偏移而位移
    [self selectedClassStateBtnClick:selectButton];
}



// 被点击的按钮
- (void)selectedClassStateBtnClick:(UIButton *)stateBtn
{
    _selectButton.selected  = NO;
    stateBtn.selected  =  YES;
    _selectButton = stateBtn;
    [stateBtn setTitleColor:zMainColor forState:UIControlStateNormal];
    //    [_selectButton setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
    // 移动下划线的位置
    [UIView animateWithDuration:0.2 animations:^{
        //转移中心点实现位移
        _lineView.xj_centerX = stateBtn.xj_centerX;
    }];
    
    // 未被点击的按钮文字置灰
    for (UIButton *btn in self.titleButtons) {
        if (btn.xj_centerX != _lineView.xj_centerX) {
            [btn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        };
    }
}



#pragma mark - <添加所有的子控制器>
- (void)setupAllChildViewController
{
    // 全部课程
    ZPAllClassViewController *allClassVc = [[ZPAllClassViewController alloc] init];
    allClassVc.title = @"全部";
    [self addChildViewController:allClassVc];
    
    // 调课
    ZPTiaoClassViewController * tiaoClassVc = [[ZPTiaoClassViewController alloc] init];
    tiaoClassVc.title = @"调课";
    [self addChildViewController:tiaoClassVc];
    
    // 转班
    ZPZhuanClassViewController * zhuanClassVc = [[ZPZhuanClassViewController alloc] init];
    zhuanClassVc.title = @"转班";
    [self addChildViewController:zhuanClassVc];
    
    // 退班
    ZPTuiClassViewController * tuiClassVc = [[ZPTuiClassViewController alloc] init];
    tuiClassVc.title = @"退班";
    [self addChildViewController:tuiClassVc];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
